import java.util.*;

public class text101 {
    public static List<String>  topKFrequent(String[]arr,int k) {
        HashMap<String, Integer> b = new HashMap<>();
        int i = 0;
        for (i = 0; i < arr.length; i++) {
            if (!b.containsKey(arr[i])) {
                b.put(arr[i], 1);
            } else {
                int val = b.get(arr[i]);
                b.put(arr[i], val + 1);
            }
        }
        Set<Map.Entry<String,Integer>>a=b.entrySet();
        PriorityQueue<Map.Entry<String, Integer>> c = new PriorityQueue<>(k, new Comparator<Map.Entry<String, Integer>>() {
            public int compare(Map.Entry<String,Integer>o1,Map.Entry<String,Integer>o2)
            {
                if(o1.getValue().compareTo(o2.getValue())==0)
                {
                    return o2.getKey().compareTo(o1.getKey());
                }
                return o1.getValue().compareTo(o2.getValue());
            }


        });
        int count=0;
        for (Map.Entry<String,Integer>l:a) {
            count++;
            if(count<=k)
            {
                c.offer(l);
            }
            else
            {
                if(l.getValue().compareTo(c.peek().getValue())>0)
                {
                    c.poll();
                    c.offer(l);
                }
                else
                {
                    if(l.getValue().compareTo(c.peek().getValue())==0)
                    {
                        if(l.getKey().compareTo(c.peek().getKey())<0)
                        {
                            c.poll();
                            c.offer(l);
                        }

                    }
                }

            }



        }
        List<String>arr2=new ArrayList<>();
        for(i=0;i<k;i++)
        {
            Map.Entry<String,Integer>cur=c.poll();
            arr2.add(cur.getKey());

        }
        Collections.reverse(arr2);
        return arr2;

    }

    public static void main(String[] args) {
        String[]arr={"the", "day", "is", "sunny", "the", "the", "the", "sunny", "is", "is"};
        List<String>m=topKFrequent(arr,4);
        System.out.println(1);
    }
}
